package com.liu.QQ.server.service;

import com.liu.QQ.Message;
import com.liu.QQ.MessageType;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;

/**
 * 持有socket和客户端保持通信
 *
 * @author liuchengjie
 */
public class ServerConnectThread extends Thread {

    private Socket socket;
    private String userId;

    public ServerConnectThread(Socket socket,String userId) {
        this.socket = socket;
        this.userId = userId;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    @Override
    public void run() {
        //和客户端保持通信
        while (true) {
            System.out.println("服务端正和客户端"+userId+"保持着通信");
            try {
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                Message message = (Message)objectInputStream.readObject();
                //判断message类型，做相应的业务处理
                //获取在线用户
                if (message.getMesType().equals(MessageType.MESSAGE_GET_ONLINE_FRIEND)) {
                    System.out.println(message.getSender() + "要在线用户列表");
                    //形式：userId userId....
                    String onlineUserList = ManageServerThread.getOnlineUserList();
                    //构建message对象返回
                    Message message2 = new Message();
                    message2.setMesType(MessageType.MESSAGE_RET_ONLINE_FRIEND);
                    message2.setContent(onlineUserList);
                    message2.setGetter(message.getSender());
                    //返回给客户端
                    ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                    outputStream.writeObject(message2);
                }
                //退出客户端
                if (message.getMesType().equals(MessageType.MESSAGE_CLIENT_EXIT)) {
                    System.out.println(message.getSender() + "客户端要退出连接");
                    //将客户端对应线程从集合中移除
                    ManageServerThread.removeThread(message.getSender());
                    socket.close();
                    //退出线程
                    break;
                }
                //私聊消息
                if (message.getMesType().equals(MessageType.MESSAGE_COMM_MES)) {
                    //获取接收方socket的输入流
                    ServerConnectThread getterThread = ManageServerThread.getConnectThread(message.getGetter());
                    if (getterThread != null) {
                        ObjectOutputStream outputStream = new ObjectOutputStream(getterThread.getSocket().getOutputStream());
                        //发送消息
                        outputStream.writeObject(message);
                    } else {
                        Message message3 = new Message();
                        message3.setMesType(MessageType.MESSAGE_SERVER);
                        message3.setContent("对方不在线，当前系统暂时不提供离线留言功能");
                        ServerConnectThread senderThread = ManageServerThread.getConnectThread(message.getSender());
                        ObjectOutputStream outputStream = new ObjectOutputStream(senderThread.getSocket().getOutputStream());
                        outputStream.writeObject(message3);
                    }
                }
                //群发消息
                if (message.getMesType().equals(MessageType.MESSAGE_DISTRIBUTION)) {
                    HashMap<String, ServerConnectThread> manage = ManageServerThread.getManage();
                    Iterator<String> iterator = manage.keySet().iterator();
                    //遍历在线用户
                    while (iterator.hasNext()) {
                        String userId = iterator.next();
                        if (!message.getSender().equals(userId)) {
                            //给在线用户发消息
                            ServerConnectThread serverConnectThread = manage.get(userId);
                            ObjectOutputStream outputStream = new ObjectOutputStream(serverConnectThread.getSocket().getOutputStream());
                            outputStream.writeObject(message);
                        }
                    }
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}

















